package com.zhao.data.config;

import java.lang.reflect.Method;
import java.util.concurrent.CountDownLatch;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import com.zhao.data.redis.DefaultMessageDelegate;
import com.zhao.data.redis.MessageDelegate;

/**
 * redis config
 * 
 * @author zhaoyongfei
 *
 */
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

	@Bean
	public KeyGenerator wiselyKeyGenerator() {
		return new KeyGenerator() {
			@Override
			public Object generate(Object target, Method method, Object... params) {
				StringBuilder sb = new StringBuilder();
				//				sb.append(target.getClass().getName());
				//				sb.append(method.getName());
				for (Object obj : params) {
					sb.append(obj.toString());
				}
				return sb.toString();
			}
		};

	}

	@Bean
	public CacheManager cacheManager(RedisTemplate<String, Object> redisTemplate) {
		return new RedisCacheManager(redisTemplate);
	}

	@Bean
	public StringRedisTemplate stringRedisTemplate() {
		StringRedisTemplate redisTemplate = new StringRedisTemplate();
		redisTemplate.setConnectionFactory(jedisConnectionFactory());
		return redisTemplate;
	}

	@Bean
	public RedisTemplate<String, Object> redisTemplate() {
		RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
		redisTemplate.setConnectionFactory(jedisConnectionFactory());
		return redisTemplate;
	}

	//	@Bean
	//	public GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer() {
	//		GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
	//		return genericJackson2JsonRedisSerializer;
	//	}

	@Bean
	public RedisConnectionFactory jedisConnectionFactory() {
		JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
		jedisConnectionFactory.setUsePool(true);
		return jedisConnectionFactory;
	}

	@Bean
	public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory jedisConnectionFactory,
			MessageListener messageListener, @Value("${spring.redis.enabled}") Boolean enabled) {
		RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
		redisMessageListenerContainer.setConnectionFactory(jedisConnectionFactory);
		redisMessageListenerContainer.setRecoveryInterval(RedisMessageListenerContainer.DEFAULT_RECOVERY_INTERVAL * 10);
		if (Boolean.valueOf(enabled)) {
			redisMessageListenerContainer.addMessageListener(messageListener, new PatternTopic("chatroom"));
		}
		return redisMessageListenerContainer;
	}

	@Bean
	public MessageListener messageListener(MessageDelegate messageDelegate) {
		MessageListener messageListener = new MessageListenerAdapter(messageDelegate);
		return messageListener;
	}

	@Bean
	public MessageDelegate messageDelegate() {
		return new DefaultMessageDelegate();
	}

	@Bean
	CountDownLatch latch() {
		return new CountDownLatch(1);
	}
}
